package com.capgemini.remind.service;

import com.capgemini.commons.HttpSMSProcessUtil;
import com.capgemini.remind.mapper.AnnouncementMapper;
import com.capgemini.remind.mapper.AnnouncementSendInfoMapper;
import com.capgemini.remind.pojo.*;
import com.capgemini.commons.IdUtils;
import com.capgemini.core.PageList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author
 * @date
 */
@Service
@Transactional
public class AnnouncementService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AnnouncementMapper announcementMapper;
	@Autowired
	private AnnouncementSendInfoService announcementSendInfoService;
	@Autowired
	private AnnouncementSendInfoMapper announcementSendInfoMapper;


	public int insert(Announcement pojo){

//		pojo.buildCreateInfo();
		Date now = new Date();

		pojo.setCreateDate(now);
		pojo.setLastUpdateDate(now);

		pojo.setId(IdUtils.getNewId());

		return announcementMapper.insert(pojo);
	}


	public int updateById(Announcement pojo){
		pojo.setLastUpdateDate(new Date());
		return announcementMapper.updateById(pojo);
	}

	public void queryPage(final PageList<Announcement> pageList) {
		pageList.setItems(announcementMapper.queryPage(pageList));
	}

	public List<Announcement> queryByDealerNum(String dealerNum) {
		return announcementMapper.queryByDealerNum(dealerNum);
	}

	public int deletePhysicalById(String id) {
		return announcementMapper.deletePhysicalById(id);
	}

	public Announcement getById(String id) {
		return announcementMapper.getById(id);
	}

	public void queryCustomerPage(final PageList<AnnouncementCustomerInfo> pageList) {
		pageList.setItems(announcementMapper.queryCustomerPage(pageList));
	}

	public void sendSms(List<AnnouncementCustomerInfo> items, String dealerNum, String announcementId, String sms) {

		List<AnnouncementSendInfo> list = new ArrayList<AnnouncementSendInfo>();

		Date now = new Date();
		for (AnnouncementCustomerInfo remind : items) {
			AnnouncementSendInfo tmp = new AnnouncementSendInfo();

			tmp.setId(IdUtils.getNewId());
			tmp.setSmsSendFlag(false);

			tmp.setSmsContent(sms);
			tmp.setAnnouncementId(announcementId);
			tmp.setDealerNum(dealerNum);

			tmp.setCustomerId(remind.getCustomerId());
			tmp.setMobilePhone(remind.getMobilePhone());
			tmp.setCustomerName(remind.getMemberName());

			tmp.setCreateBy("0");
			tmp.setCreateDate(now);

			list.add(tmp);
		}

		int beginIndex = 0;
		while (beginIndex < list.size()){

			int endIndex = 2000;
			if(list.size() - beginIndex < 2000){
				endIndex = list.size() - beginIndex;
			}

			List sub = list.subList(beginIndex,  beginIndex + endIndex);
			List two = new ArrayList(sub);
			announcementSendInfoService.batchInsert(two);
			two.clear();

			beginIndex += endIndex;
		}
		//todo 扣除短信余额
//		remindSmsService.subtractSmsQuotaByDealerNum(list.size(), dealerNum);

		//todo 批量发送短信

		int index = 0;
		List<String> mobileList = new ArrayList<String>();
		List<String> idList = new ArrayList<String>();
		for (AnnouncementSendInfo announcementSendInfo : list) {
			mobileList.add(announcementSendInfo.getMobilePhone());
			idList.add(announcementSendInfo.getId());
			index ++;
			if(index % 999 == 0){
				//发送短信
				String smsBatchId = HttpSMSProcessUtil.sendMessage(sms, mobileList);
				announcementSendInfoMapper.updateSmsBatchIdByIds(smsBatchId, idList);
				index = 0;
				mobileList.clear();
				idList.clear();
			}
		}

		if(index > 0){
			//发送短信
			String smsBatchId = HttpSMSProcessUtil.sendMessage(sms, mobileList);
			announcementSendInfoMapper.updateSmsBatchIdByIds(smsBatchId, idList);
		}
	}
}
